package com.idanchuang.income;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.YearMonth;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * 品牌月度收益比率
 */
public class BrandMonthlyRateBuilder implements Serializable {
    static final long serialVersionUID = 0L;
    private final static Random r = new Random();
    //原始的品牌月度收益占比
    private BrandInfo initBrandInfo;
    private BrandMonthlyRate[] brandRates;

    public BrandMonthlyRateBuilder() {
    }

    public void build() {
        initBrandInfo = new BrandInfo();
        try {
            randomInit();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    public List<BrandInfo.BrandIncomeMonthlyRate> getMonthlyRate(long userId, YearMonth month) {
        BrandMonthlyRate rate = brandRates[(int) (userId % brandRates.length)];
        List<BrandInfo.BrandIncomeMonthlyRate> rates = rate.getMonthlyRate(month);
        if (rates == null) {
            rates = rate.getMonthlyRate(YearMonth.of(2019, month.getMonthValue()));
        }
        return rates;
    }

    private void randomInit() throws CloneNotSupportedException {
        brandRates = new BrandMonthlyRate[10];
        for (int i = 0; i < brandRates.length; i++) {
            BrandMonthlyRate monthlyRate = new BrandMonthlyRate();
            brandRates[i]=monthlyRate;
            Map<YearMonth, List<BrandInfo.BrandIncomeMonthlyRate>> brandsMonthlyRate = initBrandInfo.getBrandsMonthlyRate();
            for (YearMonth month : brandsMonthlyRate.keySet()) {
                //重新调整品牌的占比
                List<BrandInfo.BrandIncomeMonthlyRate> rates = brandsMonthlyRate.get(month);
                rates = rates.stream().sorted().collect(Collectors.toList());
                BigDecimal[] brandRates = getArrayRandoms(rates.size());
                List<BrandInfo.BrandIncomeMonthlyRate> newRates = new LinkedList<>();
                monthlyRate.addRate(month, newRates);
                Map<String, BigDecimal[]> brandTypeRate = initBrandInfo.getBrandTypeRate();
                for (int j = 0; j < rates.size(); j++) {
                    BrandInfo.BrandIncomeMonthlyRate brandRate = (BrandInfo.BrandIncomeMonthlyRate) rates.get(j).clone();
                    brandRate.setRate(brandRate.getRate().add(brandRates[j]));
                    BigDecimal[] typeRates = brandTypeRate.get(brandRate.getName());
                    BigDecimal[] typeRatesArrange = getArrayRandoms(typeRates.length);
                    for (int k = 0; k < typeRatesArrange.length; k++) {
                        typeRatesArrange[k] = typeRatesArrange[k].add(typeRates[k]);
                    }
                    brandRate.setTypeRates(typeRatesArrange);
                    newRates.add(brandRate);
                }
            }
        }
    }

    private static BigDecimal[] getArrayRandoms(int length) {
        BigDecimal[] ratios = new BigDecimal[length];
        BigDecimal sum = new BigDecimal(0);
        int base = 100;
        for (int i = 0; i < ratios.length - 2; i++) {
            BigDecimal v;
            int f = r.nextInt(base * 2);
            if (f > base) {
                v = new BigDecimal(f - base).divide(new BigDecimal(base*100));
            } else {
                v = new BigDecimal(f).divide(new BigDecimal(base*100)).negate();
            }
            ratios[i] = v;
            sum = sum.add(v);
        }
        BigDecimal left = new BigDecimal(0).subtract(sum).divide(new BigDecimal(2));
        ratios[ratios.length - 2] = left;
        ratios[ratios.length - 1] = left;
        return ratios;
    }
}
